package eu.jpereira.trainings.designpatterns.creational.abstractfactory;

import java.util.HashMap;
import java.util.Map;

public class ReportFactory
{
    protected static Map<String, Class<? extends AbstractReportFactory>> concreteFactories = new HashMap<String, Class<? extends AbstractReportFactory>>();

    public static void registerFactory(String type,
            Class<? extends AbstractReportFactory> factory)
    {
        concreteFactories.put(type, factory);
    }

    public static Report createReport(String type)
            throws InstantiationException, IllegalAccessException
    {
        Class<? extends AbstractReportFactory> factoryClass = concreteFactories
                .get(type);
        if (factoryClass == null)
        {
            return null;
        }
        AbstractReportFactory factory = factoryClass.newInstance();
        Report result = new Report();
        result.setHeader(factory.getHeader());
        result.setBody(factory.getBody());
        result.setFooter(factory.getFooter());
        return result;
    }

}
